import { ref, onMounted, computed } from 'vue'
import { defineStore } from 'pinia'
import { getIpCity, getAdcodeWeather } from '@/api'
export const useWeatherStore = defineStore('weather', () => {
  const res = ref({})
  const ipCity = ref('')
  const ipWeatherLives = ref({})
  const ipWeatherDays = ref([])

  const chartCity = ref('')
  const chartWeatherLives = ref({})
  const chartWeatherDays = ref([])
  //ip城市实时天气部分
  onMounted(async () => {
    res.value = await getIpCity()
    //   console.log(res.value)
    ipCity.value = res.value.city
    await getWeatherData(res.value.adcode)
    await updateWeatherData(res.value.adcode)
    // lodaStorageCtiys()
    //原始,暂时不用
    //   ipWeather.value = await getAdcodeWeather(res.value.adcode, 'all')

    // ipWeatherLives.value = (
    //   await getAdcodeWeather(res.value.adcode, 'base')
    // ).lives[0]
    //   console.log(ipWeather.value.lives[0].weather)
    //   console.log(ipWeather.value)
    // ipWeatherDays.value = (
    //   await getAdcodeWeather(res.value.adcode, 'all')
    // ).forecasts[0].casts
    // console.log(ipWeatherDays.value.forecasts[0].casts)
    // console.log(ipWeatherDays.value)
  })

  //封装
  const getWeatherData = async (adcode) => {
    ipWeatherLives.value = (await getAdcodeWeather(adcode, 'base')).lives[0]
    ipWeatherDays.value = (
      await getAdcodeWeather(adcode, 'all')
    ).forecasts[0].casts
  }

  //更新,搜索用

  const updateWeatherData = async (adcode) => {
    chartWeatherLives.value = (await getAdcodeWeather(adcode, 'base')).lives
    chartWeatherDays.value = (
      await getAdcodeWeather(adcode, 'all')
    ).forecasts[0].casts
  }

  const searchCity = ref('')
  const updateAdcode = async (adcode) => {
    // console.log(searchCity.value)
    await updateWeatherData(adcode)
    searchCity.value = adcode
  }

  const isShow = ref(false)
  const updateAbout = () => {
    isShow.value = !isShow.value
  }

  //===============本地存储部分
  const storageCtiys = ref([])

  //保存的方法
  const storageCtiy = (cityAdcode) => {
    if (cityAdcode && !storageCtiys.value.includes(cityAdcode)) {
      storageCtiys.value.push(cityAdcode)
    }
    localStorage.setItem('cityAdcode', JSON.stringify(storageCtiys.value))
  }

  //读取的方法
  const isExist = ref(false)
  const lodaStorageCtiys = () => {
    const existData = localStorage.getItem('cityAdcode')
    if (existData) {
      isExist.value = true
      storageCtiys.value = JSON.parse(existData)
      // console.log(storageCtiys.value)
    }
  }

  //删除的方法
  const deleteCity = (index) => {
    storageCtiys.value.splice(index, 1)
    localStorage.setItem('cityAdcode', JSON.stringify(storageCtiys.value))
  }

  //判断本地存储是否有数据
  const isSearchExist = ref(true)
  const searchCityExist = (adcode) => storageCtiys.value.includes(adcode)
  return {
    res,
    //header固定显示用
    ipCity,
    ipWeatherLives,
    ipWeatherDays,
    //图表动态用
    searchCity,
    chartCity,
    chartWeatherLives,
    chartWeatherDays,
    //about用
    isShow,

    //存储用
    storageCtiys,
    isExist,
    isSearchExist,
    updateAbout,

    updateAdcode,

    //存储用
    storageCtiy,
    lodaStorageCtiys,
    deleteCity,
    searchCityExist,
  }
})
